# -*- coding:utf-8 -*-
import paramiko
import os

# ip = "182.92.198.113"
# passwd = "fd20Q04F858ZPb1"
ip = "47.93.58.128"
passwd = "JShic82^230~!"
t = paramiko.Transport((ip, 22))
t.connect(username="root", password=passwd)


def sftp_upload_file(server_path, local_path):
    try:
        sftp = paramiko.SFTPClient.from_transport(t)
        sftp.put(local_path, server_path)

        print(local_path, " upload file success")
    except Exception as e:
        print(e)


def cmd(cmds):
    try:
        t = paramiko.Transport(("182.92.198.113", 22))
        t.connect(username="root", password="fd20Q04F858ZPb1")
        ssh = paramiko.SSHClient()
        ssh._transport = t
        stdin, stdout, stderr = ssh.exec_command(cmds)
        result = stdout.read()
        print(result.decode())

        t.close()
    except Exception as e:
        print(e)


def upload():
    for root, dirs, files in os.walk(os.path.abspath('.') + "/data"):
        for file in files:
            if "pkl" in file and "pyc" not in file:
                sftp_upload_file("/root/pred/data/" + file, os.path.abspath('.') + "/data/" + file)
    # for root, dirs, files in os.walk(os.path.abspath('.')):
    #     for file in files:
    #         if "py" in file and "pyc" not in file:
    #             sftp_upload_file("/root/pred/" + file, os.path.abspath('.') + "/" + file)
    #
    # sftp_upload_file("/root/pred/fetchdata.py", os.path.abspath('.') + "/fetchdata.py")
    # sftp_upload_file("/root/pred/autocode_torch.py", os.path.abspath('.') + "/autocode_torch.py")
    sftp_upload_file("/root/pred/rnnflask.py", os.path.abspath('.') + "/rnnflask.py")
    sftp_upload_file("/root/pred/rnnflask2.py", os.path.abspath('.') + "/rnnflask2.py")
    # sftp_upload_file("/root/pred/rnnflask3.py", os.path.abspath('.') + "/rnnflask3.py")
    # sftp_upload_file("/root/pred/autoflask.py", os.path.abspath('.') + "/autoflask.py")
    # sftp_upload_file("/root/pred/update.py", os.path.abspath('.') + "/update.py")
    # sftp_upload_file("/root/pred/upsched.py", os.path.abspath('.') + "/upsched.py")
    # sftp_upload_file("/root/pred/excel.py", os.path.abspath('.') + "/excel.py")

    # reload = 'supervisorctl restart all'
    # cmd(reload)


if __name__ == '__main__':
    upload()
    t.close()
